﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections.ObjectModel;

namespace DownloadInterface
{
    [Serializable()]
    public class FileManage : IFileManage
    {
        public ObservableCollection<FileCategory> KfileCategory;
        public Dictionary<FileCategory, ObservableCollection<DownloadedFile>> KDictionary;
        List<String> exten_Document = new List<string>();
        List<String> exten_Music = new List<string>();
        List<String> exten_Video = new List<string>();
        List<String> exten_Program = new List<string>();
        List<String> exten_Unknown = new List<string>();


        public FileManage()
        {
            FileCategory doc = new FileCategory();
            doc.categoryId = 0;
            doc.categoryName = "文档";
            ObservableCollection<DownloadedFile> doc_FileList = new ObservableCollection<DownloadedFile>();


            FileCategory mp3 = new FileCategory();
            mp3.categoryId = 1;
            mp3.categoryName = "音乐";
            ObservableCollection<DownloadedFile> mp3_FileList = new ObservableCollection<DownloadedFile>();

            FileCategory vid = new FileCategory();
            vid.categoryId = 2;
            vid.categoryName = "视频";
            ObservableCollection<DownloadedFile> vid_FileList = new ObservableCollection<DownloadedFile>();

            FileCategory exe = new FileCategory();
            exe.categoryId = 3;
            exe.categoryName = "程序";
            ObservableCollection<DownloadedFile> exe_FileList = new ObservableCollection<DownloadedFile>();

            FileCategory unk = new FileCategory();
            unk.categoryId = 4;
            unk.categoryName = "未知分类";
            ObservableCollection<DownloadedFile> unk_FileList = new ObservableCollection<DownloadedFile>();

            KfileCategory = new ObservableCollection<FileCategory>();
            KfileCategory.Add(doc);
            KfileCategory.Add(mp3);
            KfileCategory.Add(vid);
            KfileCategory.Add(exe);
            KfileCategory.Add(unk);

            KDictionary = new Dictionary<FileCategory, ObservableCollection<DownloadedFile>>();
            KDictionary.Add(doc, doc_FileList);
            KDictionary.Add(mp3, mp3_FileList);
            KDictionary.Add(vid, vid_FileList);
            KDictionary.Add(exe, exe_FileList);
            KDictionary.Add(unk, unk_FileList);


            extenNameLoad();                               //载入后缀名文件
        }

        bool extenNameLoad()
        {
            try
            {
                using (StreamReader sr = new StreamReader("ExtenName.txt"))
                {
                    String line;
                    int i = -1;
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (line == "Document" || line == "Music" || line == "Video" || line == "Program")
                        {
                            if (line == "Document") i = 0;
                            if (line == "Music") i = 1;
                            if (line == "Video") i = 2;
                            if (line == "Program") i = 3;
                        }
                        else
                        {
                            if (i == 0) exten_Document.Add(line);
                            if (i == 1) exten_Music.Add(line);
                            if (i == 2) exten_Video.Add(line);
                            if (i == 3) exten_Program.Add(line);
                        }
                    }
                }
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        int extenNameCheck(DownloadTask newFile)
        {
            string FileName = newFile.fileName;
            String extenName = System.IO.Path.GetExtension(FileName);

            if (extenName == String.Empty)
            {
                return -1;
            }
            if (exten_Document.Contains(extenName))
            {
                return 0;
            }
            if (exten_Music.Contains(extenName))
            {
                return 1;
            }
            if (exten_Video.Contains(extenName))
            {
                return 2;
            }
            if (exten_Program.Contains(extenName))
            {
                return 3;
            }
            else
            {
                return 4;
            }
        }

        [field:NonSerialized]public event DownloadErrorEventHandler DownloadErrorEvent;

        public bool addFile(DownloadTask newFile)
        {
            int i = extenNameCheck(newFile);
            DownloadedFile waitForStore = new DownloadedFile(newFile);
           
            if (i == 0)
            {
                FileCategory a = KfileCategory[0];
                KDictionary[a].Add(waitForStore);
                return true;
            }
            if (i == 1)
            {
                FileCategory b = KfileCategory[1];
                KDictionary[b].Add(waitForStore);
                return true;
            }
            if (i == 2)
            {
                FileCategory c = KfileCategory[2];
                KDictionary[c].Add(waitForStore);
                return true;
            }
            if (i == 3)
            {
                FileCategory d = KfileCategory[3];
                KDictionary[d].Add(waitForStore);
                return true;
            }
            if (i == 4)
            {
                FileCategory e = KfileCategory[4];
                KDictionary[e].Add(waitForStore);
                return true;
            }
            return false;
            /*
            DownloadedFile waitForStore = new DownloadedFile();
            waitForStore.fileName = newFile.fileName;
            waitForStore.filePath = newFile.filePath;

            string FileName = newFile.fileName;
            if (FileName.EndsWith(".doc") || FileName.EndsWith(".docx"))         //doc分类
            {
                FileCategory a = KfileCategory[0];

                waitForStore.fileId = 0;

                KDictionary[a].Add(waitForStore);

                return true;
            }
            if (FileName.EndsWith(".mp3") || FileName.EndsWith(".wma"))            //Music 分类
            {
                FileCategory b = KfileCategory[1];

                waitForStore.fileId = 1;

                KDictionary[b].Add(waitForStore);

                return true;
            }
            if (FileName.EndsWith(".mp4") || FileName.EndsWith(".avi") || FileName.EndsWith(".mkv") || FileName.EndsWith(".rmvb"))            //video 分类
            {
                FileCategory c = KfileCategory[2];

                waitForStore.fileId = 2;

                KDictionary[c].Add(waitForStore);

                return true;
            }
            if (FileName.EndsWith(".exe") || FileName.EndsWith(".jar"))            //program 分类
            {
                FileCategory d = KfileCategory[3];

                waitForStore.fileId = 3;

                KDictionary[d].Add(waitForStore);

                return true;
            }
            else                                                                   //其他unknown分类
            {
                FileCategory e = KfileCategory[4];

                waitForStore.fileId = 4;

                KDictionary[e].Add(waitForStore);

                return true;
            }
            //return false;
           */
        }

        public bool deleteFile(FileCategory category,DownloadedFile file)
        {
            FileCategory a = category;
            bool deleted = false;
            String filename = file.filePath +"\\" + file.fileName;
            if (!KDictionary[a].Contains(file))
            {
                return false;
            }
            else
            {
                if (File.Exists(filename))
                {
                    try
                    {
                        File.SetAttributes(filename, FileAttributes.Normal);
                        File.Delete(filename);
                        deleted = true;
                    }
                    catch (Exception e)
                    {
                        deleted = false;
                    }
                }
                else {
                    DownloadErrorEvent(DownloadError.ErrorCode.DOWNLOAD_FILE_NOT_EXIST);
                    KDictionary[a].Remove(file);
                }
                if (deleted == true)
                {
                    KDictionary[a].Remove(file);
                    return true;
                }
            }
            
            return false;

        }

        public ObservableCollection<FileCategory> getFileCategoryList()
        {
            return KfileCategory;
        }

        public ObservableCollection<DownloadedFile> getDownloadedFileList(FileCategory category)
        {
            return KDictionary[category];
        }





        public void setFileType(DownloadTask task)
        {
            int i = extenNameCheck(task);
            switch (i)
            {
                case 0: task.fileType = FileType.FILE_DOCUMENT; break;
                case 1: task.fileType = FileType.FILE_MUSIC; break;
                case 2: task.fileType = FileType.FILE_MOVIE; break;
                case 3: task.fileType = FileType.FILE_EXECUTEABLE; break;
                default: task.fileType = FileType.FILE_UNKNOWN; break;
            }
        }
    }

}
